package com.molichuxing.services.infrastructure.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.bean.UserBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.UserMapper;
import com.molichuxing.services.infrastructure.dto.request.create.UserCreateDto;
import com.molichuxing.services.infrastructure.dto.response.UserDto;
import com.molichuxing.services.infrastructure.service.UserService;

/**
 * 
 * @author liyg
 * 用户服务实现类
 * 2019-08-06
 * 
 */
@Service("userService")
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userMapper;
	
	
	/**
	 * 获取用户列表
	 *
	 * @param map
	 *            参数
	 * key:name String 用户名 非必填
	 * key:realName String 用户姓名 非必填
	 * key:phone String 用户手机 非必填
	 * key:source Integer 用户来源 非必填
	 * key:startTime String 注册开始时间 非必填
	 * key:endTime String 注册结束时间 非必填
	 * key:isOrder Boolean 是否有订单 非必填
	 * pageNum Integer 当前页
	 * pageSize Integer 每页显示条数
	 *
	 */
	@Override
	public Paged<UserDto> getUserList(Map<String, Object> map) {

		// 查询总条数
		long total = userMapper.getUserPageCount(map);
		if (total <= 0) {
			return null;
		}

		/*查询列表分页数据*/
		Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
		Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
		List<UserBean> list = userMapper.getUserPage(map,pageNum,pageSize);
		List<UserDto> userDtoList = Convert.toUserDto(list);
		
		Paged<UserDto> paged = new Paged<>(userDtoList, total,pageSize);
		
		return paged;
	}

	/**
	 * 获取用户基本信息
	 * 
	 * @param ids
	 * @return
	 */
	@Override
	public Map<Integer, UserDto> getUsers(List<Integer> ids) {
		if (null == ids || ids.size() == 0) {
			return null;
		}

		List<UserBean> list = userMapper.getByIds(ids);
		List<UserDto> userDtoList = Convert.toUserDto(list);
		if (null == userDtoList || userDtoList.size() <= 0) {
			return null;
		}

		Integer initialCapacity=userDtoList.size();
		Map<Integer, UserDto> map = new HashMap<>(initialCapacity);
		for (UserDto userDto : userDtoList) {
			Integer id = userDto.getId();
			map.put(id, userDto);
		}

		return map;
	}

	/**
	 * 获取用户列表
	 * 
	 * @param name
	 *            用户名（模糊查询）非必填
	 * @param realName
	 *            用户姓名（模糊查询）非必填
	 * @param phone
	 *            用户手机号（精确查询） 非必填
	 * @return
	 */
	@Override
	public List<UserDto> getUserList(String name, String realName, String phone) {

		List<UserBean> list = userMapper.getListByNameAndPhone(name, phone, realName);
		
		List<UserDto> dtoList = Convert.toUserDto(list);
		
		return dtoList;
	}

	/**
	 * 获取用户信息
	 * @param id
	 * @return
	 */
	@Override
	public UserDto getUser(Integer id) {
		
		UserBean user = userMapper.getById(id);
		
		UserDto userBeanDto = Convert.toUserDto(user);
		
		return userBeanDto;
	}
	
	/**
	 * 获取用户信息
	 *
	 * @param openId
	 *            微信公众号 openId
	 * @return
	 */
	@Override
	public UserDto getUser(String openId) {
		
		UserBean user = userMapper.getByOpenId(openId);
		
		UserDto userBeanDto = Convert.toUserDto(user);
		
		return userBeanDto;
	}

	/**
	 * 获取用户信息
	 * 
	 * @param phone
	 *           用户手机号
	 * @return
	 */
	@Override
	public UserDto getUserByPhone(String phone) {
		if(StringUtils.isBlank(phone)) {
			return null;
		}
		return getUser(null, null, phone);
	}

	/**
	 * 获取用户信息
	 * 
	 * @param name
	 *           用户名
	 * @return
	 */
	@Override
	public UserDto getUserByName(String name) {
		if(StringUtils.isBlank(name)) {
			return null;
		}
		return getUser(name, null, null);
	}

	/**
	 * 获取用户信息
	 * 
	 * @param realName
	 *           用户真实姓名
	 * @return
	 */
	@Override
	public UserDto getUserByRealName(String realName) {
		if(StringUtils.isBlank(realName)) {
			return null;
		}
		return getUser(null, realName, null);
	}

	/**
	 * 获取用户信息
	 * 
	 * @param name
	 *            用户名（精确查询） 非必填 (必须有一项必填)
	 * @param realName
	 *            用户真实姓名（精确查询） 非必填
	 * @param phone
	 *            用户手机号（精确查询） 非必填

	 * 
	 * @return
	 */
	@Override
	public UserDto getUser(String name,String realName,String phone) {

		if (StringUtils.isBlank(name) && StringUtils.isBlank(phone) && StringUtils.isBlank(realName)) {
			throw new BizException(103, "name 和   phone 和 realName  必须有一个不为空！");
		}

		UserBean user = userMapper.getUserByNameAndPhone(name, phone, realName);
		UserDto userDto = Convert.toUserDto(user);

		return userDto;
	}
	
	/**
	 * 修改用户是否拉入黑名单状态
	 * @param id
	 * @param isBlack true：已拉黑 false：未拉黑
	 * @return
	 */
	@Override
	public Boolean modifyIsBlack(Integer id, Boolean isBlack) {
		
		int num = userMapper.updateIsBlack(id,isBlack);
		return num == 1 ? true :false;
	}
	
	/**
	 * 保存用户信息
	 * @param createDto
	 * @return
	 */
	@Override
	public Integer createUser(UserCreateDto createDto) {
		UserBean userBean = new UserBean();
		BeanUtils.copyProperties(createDto, userBean);
		userMapper.create(userBean);
		return userBean.getId();
	}
	
	/**
	 * 修改用户地址
	 * @param realName
	 * @param email
	 * @param address
	 * @param userId
	 * @return
	 */
	@Override
	public Boolean modifyUser(String realName, String email, String address, Integer userId) {
		
		int num = userMapper.modifyUser(realName,email,address,userId);
		return num == 1 ? true :false;
	}
	
	/**
	 * 修改用户个人信息
	 * @param realName
	 * @param userId
	 * @return
	 */
	@Override
	public Boolean modifyUserRealName(String realName, Integer userId) {
		int num = userMapper.modifyUserRealName(realName,userId);
		return num == 1 ? true :false;
	}
	
	/**
	 * 修改用户微信openId
	 * @param openId
	 * @param userId
	 * @return
	 */
	@Override
	public Boolean modifyUserOpenId(String openId, Integer userId) {
		int num = userMapper.modifyUserOpenId(openId,userId);
		return num == 1 ? true :false;
	}
	
	/**
	 * 修改用户微信为空
	 * @param openId
	 * @return
	 */
	@Override
	public Boolean modifyUserOpenId(String openId) {
		userMapper.modifyUserByOpenId(openId);
		return true;
	}
	
	/**
	 * 修改用户地址+邮箱
	 * @param address
	 * @param email
	 * @param userId
	 * @return
	 */
	@Override
	public Boolean modifyUserEmail(String address, String email, Integer userId) {
		int num = userMapper.modifyUserEmail(address,email,userId);
		return num == 1 ? true :false;
	}
	
	/**
	 * 修改用户地址
	 * @param address
	 * @param userId
	 * @return
	 */
	@Override
	public Boolean modifyUserAddress(String address, Integer userId) {
		int num = userMapper.modifyUserAddress(address,userId);
		return num == 1 ? true :false;
	}
	
	/**
	 *  修改用户是否下过单  true：下过单  false：未下过单
	 * @param isOrder
	 * @param userId
	 * @return
	 */
	@Override
	public Boolean modifyUserIsOrder(Boolean isOrder, Integer userId) {
		int num = userMapper.modifyUserIsOrder(isOrder,userId);
		return true;
	}
}
